<template>
  <div class="setting-yly-container">
    <!-- 操作 -->
    <el-row class="setting-yly-row">
      <el-col :span="14" style="text-align:left;padding-left:15px" class="setting-yly-row-col">
        <el-button type="primary" size="small" style="padding:8px 30px;" @click="add">新 增</el-button>
      </el-col>
      <el-col :span="10" style="text-align:right;padding-right:40px">
        <el-input v-model="searchName" placeholder="请输入名称" size="small" style="max-width:240px" @keydown.enter="search">
          <el-button slot="suffix" type="text" size="small" class="search-button" @click="search">
            <i class="el-icon-search"></i>
          </el-button>
        </el-input>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <div>
      <el-table :data="ylyList" style="width:100%" stripe>
        <el-table-column prop="name" label="打印机名称" align="center"></el-table-column>
        <el-table-column prop="apikey" label="API密钥" align="center"></el-table-column>
        <el-table-column prop="machine_code" label="打印机终端号" align="center"></el-table-column>
        <el-table-column prop="msign" label="打印机终端密钥" align="center"></el-table-column>
        <el-table-column prop="partner" label="易联云用户ID" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope" width="80px">
            <el-switch v-model="scope.row.status" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text" 
              @click="eidt(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button class="action-button" 
              @click="del(scope.row)" type="text">
              <svg-icon icon-class="shanchu"/>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <!-- 新增 编辑 -->
    <el-dialog :visible.sync="disAdd" width="30%" :title="isAdd ? '新增' : '修改'">
      <add :isAdd="isAdd" :addData="ylyData" @success="disAdd = false; getYLYList()" v-if="disAdd"/>
    </el-dialog>
  </div>
</template>

<script>
import { getYLYList, putYLY, delYLY } from '@/api/setting'
import add from './components/add'

export default {
  name: 'setting-yly',
  components: {
    add
  },
  data() {
    return {
      page: 1,
      count: 1,

      ylyList: [],
      searchName: '',

      disAdd: false,
      isAdd: true,

      ylyData: {
        key: this.$store.state.app.activeApp.saa_key,
        name: '',
        apikey: '',
        machine_code: '',
        msign: '',
        partner: ''
      }
    }
  },
  mounted() {
    this.getYLYList();

  },
  methods: {
    /**
     * 获取团长等级列表
     */
    getYLYList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10
      };
      getYLYList(params).then(res => {
        if(res.status === 200){
          this.ylyList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.ylyList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getYLYList();
    },
    /**
     * 搜索
     */
    search() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: 1,
        limit: 10,
        searchName: this.searchName
      };
      getYLYList(params).then(res => {
        if(res.status === 200){
          this.ylyList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.ylyList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 新增
     */
    add() {
      this.ylyData = {
        key: this.$store.state.app.activeApp.saa_key,
        name: '',
        apikey: '',
        machine_code: '',
        msign: '',
        partner: ''
      };
      this.isAdd = true;
      this.disAdd = true;
    },
    /**
     * 删除
     */
    del(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id
      }
      delYLY(params).then(response => {
        if(response.status === 200){
          this.getYLYList();
          this.$message.success('删除成功！');
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 编辑
     */
    eidt(data) {
      this.ylyData = {
        key: this.$store.state.app.activeApp.saa_key,
        name: data.name,
        apikey: data.apikey,
        machine_code: data.machine_code,
        msign: data.msign,
        partner: data.partner,
        id: data.id
      };
      this.disAdd = true;
      this.isAdd = false;
    },
    /**
     * 修改状态
     */
    changeStatus(data) {
      const params = { key: this.$store.state.app.activeApp.saa_key, id: data.id, status: data.status };
      putYLY(params).then(res => {
        if(res.status === 200){
          this.$message.success('修改成功！');
        }else{
          this.$message.error(res.message);
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.setting-yly-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.setting-yly-row{
  margin: 15px 0;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 95%;
    overflow: hidden;
    display: flex;
  }
}
</style>